package com.efast.cafe.portal.controller.api;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.vo.DataTableVo;
import com.efast.cafe.portal.bean.api.ApiObjDefQueryBean;
import com.efast.cafe.portal.bean.api.ApiRequestUriBean;
import com.efast.cafe.portal.entity.api.ApiObjDef;
import com.efast.cafe.portal.service.api.IApiObjDefService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

import net.sf.json.JSONObject;
@Controller
@RequestMapping("/frame_center/api")
public class ApiObjDefController extends AbstractController {

	@Autowired
	private IApiObjDefService apiObjDefService;
	
	
	/**
	 * 页面跳转
	 * @param searchValue
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/obj/page")
	public String templatePage(@RequestParam(required = false) String searchValue, ModelMap model,
			HttpServletRequest request, HttpServletResponse response){
		
		return "/api/obj/list";
	}
    
	/**
	 * 查询uri 对象列表
	 * 
	 * @param queryBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/obj/list")
	public @ResponseBody DataTableVo<ApiObjDef> templatePage(@ModelAttribute ApiObjDefQueryBean queryBean,
			HttpServletRequest request, HttpServletResponse response) {

		return outPageJSONArray_bootstrapDatatable(apiObjDefService.queryPageList(queryBean));

	}
	
	/**
	 * 获取相关uri关联对象 列表以及未关联对象列表
	 * uriSelected   uri关联对象列表
	 * uriUnSelected  未关联对象列表
	 */
	@RequestMapping(value = "/obj/urimultiple")
	public @ResponseBody Map<String, List<ApiObjDef>> multiple(ApiObjDefQueryBean queryBean){
		
		return apiObjDefService.multiple(queryBean);
	}
     
	/**
	 * 新增对象
	 * @param apiObjDef
	 * @param request
	 * @param response
	 * @return
	 */
	//@RequestMapping(value = "/obj/add")
	public @ResponseBody ResultBean addApiObj(@ModelAttribute ApiObjDef apiObjDef, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(ADD_SUCCESS_TIP));
		Integer id = null;
		try {

			id = apiObjDefService.addApiObj(apiObjDef);
			result.setObject(id.toString());
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}

		return result;
	}

	/**
	 * 编辑对象
	 * @param apiObjDef
	 * @param request
	 * @param response
	 * @return
	 */
	//@RequestMapping(value = "/obj/edit")
	public @ResponseBody ResultBean editApiObj(@ModelAttribute ApiObjDef apiObjDef, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(EDIT_SUCCESS_TIP));
		try {

			apiObjDefService.editApiObj(apiObjDef);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(EDIT_FAILURE_TIP));
			}
		}

		return result;
	}

	/**
	 * 通过ID删除对象
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	//@RequestMapping(value = "/obj/del")
	public @ResponseBody ResultBean delApiObj(@RequestParam(required = false) Integer id, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(DEL_SUCCESS_TIP));
		try {
			apiObjDefService.delApiObj(id);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(DEL_FAILURE_TIP));
			}
		}
		return result;
	}


	/**
	 * 跳转编辑或者新增页面
	 * @param uriId
	 * @param moduleVersion
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	//@RequestMapping(value = "/obj/detail")
	public String formPage(@ModelAttribute ApiObjDef apiObjDef, 
			HttpServletRequest request, 
			HttpServletResponse response,ModelMap model){
		if(apiObjDef.getId()!=null){
			apiObjDef= apiObjDefService.getApiObjDetail(apiObjDef.getId());
		}
		JSONObject ajaxsJson = JSONObject.fromObject(apiObjDef);
		model.addAttribute("ajaxsJson", ajaxsJson);
		return "/api/obj/detail";
	}
}
